<?php

namespace addon\lian\model;

use addon\lian\model\sqlmodel\LianRewardModel;
use app\model\BaseModel;
use think\facade\Log;

class LianReward extends BaseModel
{
    public function getPage($site_id, $condition, $page, $page_size){
        try {
            $order = 'id desc';
            $field = '*';

            $search_model = (new LianRewardModel())->alias('lian_reward')
                ->where('site_id', $site_id)
                ->where($condition)
                ->field($field)
                ->with([
                    'lianOrderGoods' => function($query){
                        $query->withField('goods_id,goods_name,goods_image,price');
                    },
                    'lianOrder' => function($query){
                        $query->withField('order_id,order_no,order_money,pay_time');
                    },
                    'buyMember' => function($query){
                        $query->withField('member_id,nickname,headimg,mobile');
                    },
                    'lianLevel' => function($query){
                        $query->withField('level_id,level_name');
                    },
                    'member' => function($query){
                        $query->withField('member_id,nickname,headimg,mobile');
                    }
                ])
                ->order($order);

            $list = $search_model->paginate([
                'list_rows' => $page_size,
                'page' => $page
            ]);

            $result = [];
            foreach ($list->items() as $item) {
                $data = $item->toArray();
                $data['frozen_amount'] = number_format($data['frozen_amount'], 2);
                $data['give_amount'] = number_format($data['give_amount'], 2);
                $data['settle_amount'] = number_format($data['settle_amount'], 2);

                $data['member_info'] = [
                    'member_id' => $data['member_id'],
                    'nickname' => $data['member']['nickname'],
                    'headimg' => $data['member']['headimg'],
                    'mobile' => $data['member']['mobile']
                ];

                $data['level_info'] = [
                    'level_id' => $data['lianLevel']['level_id'],
                    'level_name' => $data['lianLevel']['level_name']
                ];

                $result[] = $data;
            }

            $info = [
                'code' => 0,
                'message' => 'SUCCESS',
                'data' => [
                    'count' => $list->total(),
                    'list' => $result ?: [], 
                    'page' => $page,
                    'page_size' => $page_size
                ]
            ];
            
            return $info;
        } catch (\Throwable $th) {
            Log::error('LianReward getPage error: ' . $th->getMessage());
            return [
                'code' => 1,
                'message' => $th->getMessage(),
                'data' => [
                    'count' => 0,
                    'list' => [],
                    'page' => $page,
                    'page_size' => $page_size
                ]
            ];
        }
    }
    
    /**
     * 获取奖励记录详情
     * @param int $site_id 站点ID
     * @param int $id 记录ID
     * @return array
     */
    public function getRewardDetail($site_id, $id)
    {
        $condition = [
            ['site_id', '=', $site_id],
            ['id', '=', $id]
        ];
        
        $info = (new LianRewardModel())->where($condition)
            ->with([
                'lianOrderGoods' => function($query){
                    $query->withField('goods_id,goods_name,goods_image,price');
                },
                'lianOrder' => function($query){
                    $query->withField('order_id,order_no,order_money,pay_time');
                },
                'buyMember' => function($query){
                    $query->withField('member_id,nickname,headimg,mobile');
                },
                'lianLevel' => function($query){
                    $query->withField('level_id,level_name');
                },
                'member' => function($query){
                    $query->withField('member_id,nickname,headimg,mobile');
                }
            ])
            ->find();
            
        return $this->success($info);
    }


    /**
     * 删除链动2+1奖励
     * @param int $id
     * @return bool
     */
    public function del($site_id, $id)
    {
        $model = (new LianRewardModel())->where([['id', '=', $id],['site_id', '=', $site_id]])->find();
        $res = $model->delete();
        return $this->success($res, 'success');
    }
}